//项目入口文件
const express = require('express');
const bp = require('body-parser');
const path = require('path');
const mount = require('mount-routes');

const app = express();
 
 app.use(bp.urlencoded({extend: true}))
 app.use(bp.json())
 
 // 设置跨域和相应数据格式
 app.all('/*', function(req, res, next) {
   res.header('Access-Control-Allow-Origin', '*')
   res.header('Access-Control-Allow-Headers', 'X-Requested-With, mytoken')
   res.header('Access-Control-Allow-Headers', 'X-Requested-With, Authorization')
   res.setHeader('Content-Type', 'application/json;charset=utf-8')
   res.header('Access-Control-Allow-Headers', 'Content-Type,Content-Length, Authorization, Accept,X-Requested-With')
   res.header('Access-Control-Allow-Methods', 'PUT,POST,GET,DELETE,OPTIONS')
   res.header('X-Powered-By', ' 3.2.1')
   if (req.method == 'OPTIONS') res.send(200)
   /*让options请求快速返回*/ else next()
 })
 
 //引入所有的路由
 mount(app,path.join(process.cwd(),'/router'),true)
 
 //开启web服务器
 app.listen(8890)
 module.exports = app;